<?php
namespace app\index\controller;
use think\Paginator;
use app\common\controller\Homebase;

class User extends Homebase
{ 
    public function staff(){
    	$name = $this->request->param('name');
    	$mobile = $this->request->param('mobile');
    	$user_count = db('user');
    	$user = db('user');
    	$query = [];
    	if($mobile){
    		$user_count->where('mobile','like','%'.$mobile.'%');
    		$user->where('a.mobile','like','%'.$mobile.'%');
    		$query['mobile'] = $mobile;
    	}
		if($name){
    		$user_count->where('name','like','%'.$name.'%');
    		$user->where('a.name','like','%'.$name.'%');
    		$query['name'] = $name;
    	}
    	$count = $user_count->where('status',1)->where('group_id','<>',2)->count();
    	$data = $user->alias('a')->join('tf_auth_group g','a.group_id = g.id','LEFT')->where('a.status',1)->where('a.group_id','<>',2)->field('a.*,g.title')->paginate(10,$count,['query'=>$query]);
    	$this->assign('data',$data);
        return $this->fetch();
    }

    public function add(){
    	if($this->request->isPost()){
    		$data = $this->request->param();
    		$isset = db('user')->where('status',1)->where('mobile',$data['mobile'])->count();
    		if($isset){
    			return ['code'=>0,'msg'=>'该手机号码已存在'];
    		}
    		if(!$data['password']){
    			$data['password'] = 123456;
    		}
    		$data['password'] = md5($data['password']);
    		$data['username'] = $data['mobile'];
    		$data['create_time'] = $data['update_time'] = time();
    		$data['reg_ip'] = $this->request->ip();
    		$res = db('user')->insert($data);
    		if($res){
    			return ['code'=>1,'msg'=>'添加成功'];
    		}else{
    			return ['code'=>0,'msg'=>'添加失败'];
    		}
    	}
    	//获取权限分组
    	$group = db('auth_group')->where('status',1)->where('id','<>',2)->select();
    	$this->assign('group',$group);
    	return $this->fetch();
    }

    public function edit(){
    	if($this->request->isPost()){
    		$data = $this->request->param();
    		$res = db('user')->update($data);
    		if($res!==false){
    			return ['code'=>1,'msg'=>'修改成功'];
    		}
    		return ['code'=>0,'msg'=>'修改失败'];
    	}
        $id = $this->request->param('id');
        $user = db('user')->where('id',$id)->find();
		$group = db('auth_group')->where('status',1)->where('id','<>',2)->select();
    	$this->assign('group',$group);
    	$this->assign('user',$user);
        return $this->fetch();
    }

    function del(){
    	if($this->request->isPost()){
    		$id = $this->request->param('id');
    		$res = db('user')->where('id',$id)->update(['status'=>0]);
     		if($res!==false){
    			return ['code'=>1,'msg'=>'删除成功'];
    		}
    		return ['code'=>0,'msg'=>'删除失败'];   		
    	}
    }

    //打手管理
    function member(){
    	$name = $this->request->param('name');
    	$mobile = $this->request->param('mobile');
    	$user_count = db('user');
    	$user = db('user');
    	$query = [];
    	if($mobile){
    		$user_count->where('mobile','like','%'.$mobile.'%');
    		$user->where('a.mobile','like','%'.$mobile.'%');
    		$query['mobile'] = $mobile;
    	}
		if($name){
    		$user_count->where('name','like','%'.$name.'%');
    		$user->where('a.name','like','%'.$name.'%');
    		$query['name'] = $name;
    	}
    	$count = $user_count->where('status',1)->where('group_id',2)->count();
    	$data = $user->alias('a')->join('tf_auth_group g','a.group_id = g.id','LEFT')->where('a.status',1)->where('a.group_id',2)->field('a.*,g.title')->paginate(10,$count,['query'=>$query]);
    	$this->assign('data',$data);
        return $this->fetch();
    }

    function memberadd(){
    	if($this->request->isPost()){
    		$data = $this->request->param();
    		$isset = db('user')->where('status',1)->where('username',$data['username'])->count();
    		if($isset){
    			return ['code'=>0,'msg'=>'该账户已存在'];
    		}
    		if(!$data['password']){
    			$data['password'] = 123456;
    		}
    		$data['password'] = md5($data['password']);
    		$data['create_time'] = $data['update_time'] = time();
    		$data['reg_ip'] = $this->request->ip();
    		$data['group_id'] = 2;
    		$res = db('user')->insert($data);
    		if($res){
    			return ['code'=>1,'msg'=>'添加成功'];
    		}else{
    			return ['code'=>0,'msg'=>'添加失败'];
    		}
    	}
    	return $this->fetch();
    }

    public function memberedit(){
    	if($this->request->isPost()){
    		$data = $this->request->param();
    		$res = db('user')->update($data);
    		if($res!==false){
    			return ['code'=>1,'msg'=>'修改成功'];
    		}
    		return ['code'=>0,'msg'=>'修改失败'];
    	}
        $id = $this->request->param('id');
        $user = db('user')->where('id',$id)->find();
    	$this->assign('user',$user);
        return $this->fetch();
    }

    function memberdel(){
		if($this->request->isPost()){
			$id = $this->request->param('id');
			$res = db('user')->where('id',$id)->update(['status'=>0]);
	 		if($res!==false){
				return ['code'=>1,'msg'=>'删除成功'];
			}
			return ['code'=>0,'msg'=>'删除失败'];   		
		}
	}

    public function userinfo(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($data['age'] && !is_numeric($data['age'])) {
                return ['code'=>0,'msg'=>'年龄必须为数字'];  
            }
            if ($data['qq'] && !is_numeric($data['qq'])) {
                return ['code'=>0,'msg'=>'qq必须为数字'];  
            }
            db('user')->where('id',session('userinfo.id'))->update($data);
            return ['code'=>1,'msg'=>'更新成功'];   
        }else{
            $data = db('user')->where('id',session('userinfo.id'))->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
    }

    public function password(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!db('user')->where(['id'=>session('userinfo.id'),'password'=>md5($data['password'])])->find()) {
                return ['code'=>0,'msg'=>'密码错误']; 
            }else{
                db('user')->where('id',session('userinfo.id'))->update(['password'=>md5($data['new_password'])]);
                return ['code'=>1,'msg'=>'修改成功'];
            }
        }else{
            return $this->fetch();
        }
    }


    public function long(){
        $where = array();
        $id = session('userinfo.id');
        if ($id != 1) {
            $where['uid'] = $id;
        }
        $datalist = Db('login_log')->where($where)->order('id desc')->paginate(20);
        $this->assign('datalist',$datalist);
        return $this->fetch();
    }

    //删除
    public function dellong(){
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            $res = db('login_log')->where('id',$id)->delete();
            if($res!==false){
                return ['code'=>1,'msg'=>'删除成功'];
            }else{
                return ['code'=>0,'msg'=>'删除失败'];
            }
        }
    }
    //批量删除
    public function dellongs(){
        $data = $this->request->post();
        $res = db('login_log')->where('id','in',$data['ids'])->delete();
        if($res!==false){
            return ['code'=>1,'msg'=>'删除成功'];
        }else{
            return ['code'=>0,'msg'=>'删除失败'];
        }
    }
}
